# -*- coding:UTF-8 -*-
import os
import scene.scene_base as scene_base
import pygame
import pytmx
import actor.actor as actor
import actor.auto_actor as auto_actor

from pytmx.util_pygame import load_pygame

class Castle(scene_base.Scene):
    # 设置tmx路径

    def set_tmx_path(self):
        self.path = os.path.join('resource','tmx', "castle.tmx")

    # 初始化场景中的对象
    def init_object(self):
        for group in self.game_map.objectgroups:
            if isinstance(group, pytmx.pytmx.TiledObjectGroup):  #pytmx.pytmx.TiledObject(
                if group.name == 'gate' : #门
                    for obj in group:
                        gate = actor.Actor(obj.x, obj.y,'', obj.name, obj.width, obj.height,);
                        self.portals_group.append(gate)
                elif group.name == 'path_breaks':  #编辑
                    for obj in group:
                        blocker = actor.Actor(obj.x, obj.y, '', 'blocker', obj.width, obj.height);
                        self.blockers_group.append(blocker)
                elif group.name == 'hero' : #英雄
                    for obj in group:
                        self.player.rect.x = obj.x
                        self.player.rect.y = obj.y
                else : #路人#商人 #智者
                    for obj in group:
                        man = auto_actor.AutoActor(obj.x, obj.y, obj.dir, obj.name, obj.name);
                        man.init_self()
                        self.sprites_group.append(man)


    # 初始话对话
    def init_dialog(self):
        self.dialogs = ['trader',{'hi,时局不稳','我已经不做生意了！'},
                        'character',{'hi,我不认识你', '有问题，问智者！'},
                        'delegator',{'将军，国王被绑架','我需要您的帮助','去营救国王!'}]
        
        
    def update(self,keys,mouse):
        '''
        场景中角色更新
        :return: 无
        '''
        for sprite in self.sprites_group:
           sprite.update()

        # self.player.update()

        # hit_actor = self.check_for_actor()
        # hit_blocker = self.check_for_blockers()
        # if (len(hit_blocker) + len(hit_actor) == 0):
        #     self.player.action(keys, None)
        # elif len(hit_actor) > 0:
        #     actor = hit_actor[0]
        #     self.curr_dialog = True
        #     if actor.name == 'character':


    def check_for_blockers(self):
        hit_blocker = pygame.sprite.spritecollide(self.player, self.blockers_group, True)
        return hit_blocker
    def check_for_actor(self):
        hit_actor = pygame.sprite.spritecollide(self.player, self.sprites_group, True)
        return hit_blocker

